<template>
  <div id="app">

    <Print />

  </div>
</template>

<script>
import Print from "./components/Print";
import {ipcRenderer} from "electron";
export default {
  name: 'App',
  components: {
    Print
  },
  data: function () {
    return {
      timer: null,
      sendTime: 0,
      doc: null
    }
  },
  created() {

    const that = this

    that.regeditDoc()

    that.timer = setInterval(
      () => {
        that.sendDoc()
      }, 500)
  },
  mounted() {
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer)
    }
  },
  methods: {
    regeditDoc() {
      const that = this
      const { ipcRenderer } = require('electron')

      ipcRenderer.on('printNotice', (event, args) => {
        console.log('打印完成: ', args.code)
        if (args && args.args && args.args._id) {
          that.$db.update(
            { _id: args.args._id },
            { $set: { status: args.code === 200 ? 1 : 2, print_time: Math.round(new Date() / 1000), print_result: JSON.stringify(args)}},
            {},
            function (err, num) {
              that.sendTime = 0
              if (that.doc && that.doc._id === args.args._id) {
                that.doc = null
              }
              that.$bus.emit('updateDocs')
            })
        }
      })
    },
    sendDoc() {
      const that = this
      let cTime = Math.round(new Date() / 1000)
      if (that.sendTime && cTime - that.sendTime < 2) {
        return
      }

      that.sendTime = Math.round(new Date() / 1000)

      that.$db.findOne(
        { status: 0, start_time: { $gt: 0 }, print_time: 0 }, function (err, doc) {
          if (err) {
            console.log('db err: ', err)
          }

          if (doc && doc.start_time &&  Math.round(new Date() / 1000) - doc.start_time > 600) {
            // 将当前文档默认为失败
            that.$db.update({_id: doc._id}, {$set: {status: 2, print_result: JSON.stringify({code: 101, msg: '打印超时了'})}}, {}, function (err, num) {
              console.log('文档标记为打印失败')
              that.$bus.emit('updateDocs')
            })
            that.doc = doc = null
          }

          if (!doc) {
            // 当前没有正在打印的文档
            // 查询当前是否存在正在待打印的文档
            that.$db.findOne({ status: 0, start_time: 0, print_time: 0 }, function (err, doc) {
              if (err) {
                console.log('db err: ', err)
              }
              if (doc) {
                that.$db.update({_id: doc._id}, { $set: {start_time: Math.round(new Date() / 1000)}}, {}, function (err, num) {
                  if (err) {
                    console.log('更新记录错误,发送失败: ', err)
                  }
                  if (num) {
                    that.doc = doc
                    console.log('发送文档: ', doc)
                    ipcRenderer.send('toPrint', doc)
                    that.$bus.emit('updateDocs')
                  }
                })
              }
            })
          }
        }
      )
    },
    addInfo() {
      const info = {
        status: 0,
        msg: JSON.stringify({
          id: 100,
          isSilent: true, // 是否静默打印
          type: 'img',
          url: 'https://hx168.oss-cn-shenzhen.aliyuncs.com/hxy_new/sf/20220702/sf_SF1358428603828.png',
          printer: this.printer
        }),
        create_time: Math.round((new Date() / 1000)),
        start_time: 0,
        print_time: 0,
        print_result: ''
      }

      this.$db.insert(info, function (err, newDoc) {
        if (!err) {
          console.log('保存成功: ', newDoc)
        }
      })
    },
    startWs() {
      console.log('app', window.process.versions)
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
